package com.hoppinzq.service.loadBalance;

import com.hoppinzq.service.bean.ZqServerConfig;
import com.hoppinzq.service.bean.ServiceWrapperRPC;
import com.hoppinzq.service.cache.OuterRPCServiceStore;
import com.hoppinzq.service.common.UserPrincipal;
import com.hoppinzq.service.interfaceService.RegisterService;
import com.hoppinzq.service.proxy.ServiceProxyFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;

import javax.annotation.PostConstruct;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author:ZhangQi
 * 负载均衡配置
 */
@ConditionalOnWebApplication
public class LoadBalanceConfig {
    private static Logger logger = LoggerFactory.getLogger(LoadBalanceConfig.class);
    @Autowired
    private ZqServerConfig zqServerConfig;
    public static boolean isLoadBalance=false;

    @PostConstruct
    public void init() {
        logger.debug("负载均衡模块加载");
        isLoadBalance=true;
        LoadBalanceCache loadBalanceCache=new LoadBalanceCache(zqServerConfig);
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        Runnable task = new Runnable() {
            @Override
            public void run() {
                loadBalanceCache.loadService();
                logger.debug("负载均衡模块加载缓存服务");
            }
        };
        executorService.scheduleAtFixedRate(task, 0, 30, TimeUnit.SECONDS);
    }
}